import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chen.haogang
 * 
 */
public class MilkShakes1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(""
					.getClass().getResourceAsStream("/B-small-practice.in")));
			int testCaseNumber = Integer.valueOf(br.readLine());
			for(int i = 0; i < testCaseNumber; i++) {
				customerFlavors = new ArrayList<List<Milkshake>>();
				flavorNumber = Integer.valueOf(br.readLine());
				List<Integer> flavorTypes = new ArrayList<Integer>();
				for(int j = 0; j < flavorNumber; j++) {
					flavorTypes.add(-1);
				}
				int customers = Integer.valueOf(br.readLine());
				List<Integer> conflictFlavor = new ArrayList<Integer>();
				for(int j = 0; j < customers; j++) {
					String[] flavors = br.readLine().split(" ");
					List<Milkshake> temp = new ArrayList<Milkshake>();
					boolean isConflict = false;
					for (int k = 0; k < Integer.valueOf(flavors[0]); k++) {
						int flavorType = new Integer(flavors[2 * k + 1]) - 1;
						if(flavorTypes.get(flavorType) < 0) {
							flavorTypes.set(flavorType, new Integer(flavors[2 * k + 2]));
						} else if(!flavorTypes.get(flavorType).equals(new Integer(flavors[2 * k + 2]))) {
							conflictFlavor.add(flavorType);
							isConflict = true;
						}
						temp.add(new Milkshake(new Integer(flavors[2 * k + 1]), new Integer(flavors[2 * k + 2])));
					}
					if (isConflict) {
						customerFlavors.add(temp);
					}
				}
				if(conflictFlavor.size() == 0) {
					System.out.println(flavorTypes);
				} else {
					
				}
				System.out.println(results);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void chooseAnother(int flavor, int malted) {
		Milkshake milkshake = new Milkshake(flavor, malted);
		for(int i = 0; i < customerFlavors.size(); i++) {
			if(customerFlavors.get(i).contains(milkshake)) {
				customerFlavors.get(i).remove(milkshake);
			}
		}
		
	}

	private static boolean canChooseAnother(int flavor, int malted) {
		for(int i = 0; i < customerFlavors.size(); i++) {
			if(customerFlavors.get(i).contains(new Milkshake(flavor, malted)) && customerFlavors.get(i).size() == 1) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 
	 */
	private static int flavorNumber;
	/**
	 * all customers' favorite milkshakes flavor
	 */
	private static List<List<Milkshake>> customerFlavors;

	/**
	 * all possible results
	 */
	private static List<List<Integer>> results;

	private static void getPossibleFlavors(List<Integer> flavorTypes, int flavorType,
			int customerFlavorIndex, List<Integer> result) {
		for(int i = flavorType; i < flavorTypes.size(); i += 2) {
			if(flavorTypes.get(i) > 0 && flavorTypes.get(i + 1) > 0) {
				
			}
		}
	}
	
	static class Milkshake implements Comparable<Milkshake> {
		int flavor;
		int malted;
		
		public Milkshake(int flavor, int malted) {
			this.flavor = flavor;
			this.malted = malted;
		}
		
		public boolean equals(Object o) {
			return flavor == ((Milkshake)o).flavor;
		}
		
		@Override
		public int compareTo(Milkshake o) {
			if(flavor == o.flavor)
				return 0;
			else if (flavor < o.flavor)
				return -1;
			else
				return 1;
		}
		public String toString() {
			return flavor + "," + malted;
		}
	}
}
